<template>
  <div class="farmland-page f-c">
    <div class="f-r j-b">
      <el-form inline class="f-g">
        <el-form-item label="田分组">
          <el-input v-model="ruleForm.value" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-button style="margin-bottom: 18px" type="primary" icon="el-icon-plus" @click="add">新增田</el-button>
    </div>
    <div class="border" />
    <div class="content f-g f-c">
      <div class="header f-r a-c j-b">
        <div v-if="showFarmland" class="f-r a-c">
          <div class="top-back" @click="onBack">
            <i class="el-icon-arrow-left" />
            返回
          </div>
          <span class="group-name">当前分组：{{ currentDetail.groupName }}</span>
          <span class="title">田块个数：</span>
          <span class="value">{{ currentDetail.fieldCount }}块</span>
          <span class="title">田块总面积：</span>
          <span class="value">{{ currentDetail.areaSum }}亩</span>
        </div>
        <div v-else />
        <SwitchTabs
          v-model="componentName"
          style="margin-right: 0"
          :data="[{ name: '列表模式', value: 'FarmlandList'},{ name: '田分组', value: 'FarmlandGroup'},{ name: '地图模式', value: 'FarmlandMap'}, ]"
          @change="(val) => $emit('change', val)"
        />
      </div>
      <Farmland v-if="showFarmland" :group-id="currentDetail.id" />
      <div v-else class="f-g f-c">
        <div class="row-list f-r f-w f-g">
          <div class="row-box">
            <div class="row-item row-item1 f-c a-c j-c" @click="onAddGroup">
              <img src="@/assets/file-fold.png">
              <div class="title">
                新增组
              </div>
            </div>
          </div>
          <div v-for="item in tableData" :key="item.id" class="row-box" @click="editGroupDetail(item)">
            <div class="row-item f-c">
              <div class="title-box" :style="{backgroundImage: `url(${item.imgUrl})`}" />
              <div class="name-wrapper">
                <div class="name">组名：{{ item.groupName }}</div>
                <div class="name">田块个数：{{ item.fieldCount }}</div>
                <div class="desc f-r j-b">
                  星级：
                  <div class="f-r" style="margin-right: 10px">
                    <el-button size="mini" @click.stop="onDelete(item)">删除</el-button>
                    <el-button size="mini" type="primary" @click.stop="editGroup(item)">修改</el-button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="page-box">
          <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
          />
        </div>
      </div>
    </div>
    <el-drawer
      title="修改组"
      :visible.sync="drawer"
      direction="rtl"
      size="500px"
    >
      <div class="drawer-content">
        <el-form ref="formRef" :model="ruleForm" label-position="top">
          <el-form-item required prop="groupName" label="分组名称">
            <el-input v-model="ruleForm.groupName" clearable placeholder="请输入分组名称" />
          </el-form-item>
          <el-form-item required prop="imgUrl" label="封面上传">
            <div style="width: 100%">
              <DragUpload style="width: 100%" :url="ruleForm.imgUrl" @on-success="url => ruleForm.imgUrl = url" />
            </div>
          </el-form-item>

          <el-form-item required prop="directorUserName" label="负责人">
            <el-input v-model="ruleForm.directorUserName" clearable placeholder="请输入负责人" />
          </el-form-item>
          <el-form-item required prop="phone" label="联系电话">
            <el-input v-model="ruleForm.phone" clearable placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="所属区域" prop="regionCode">
            <el-cascader
              v-model="ruleForm.regionCode"
              style="width: 100%"
              clearable
              :options="regionList"
              :props="{ value: 'id', label: 'label'}"
            />
          </el-form-item>
          <el-form-item required prop="remarks" label="备注">
            <el-input v-model="ruleForm.remarks" clearable type="textarea" :rows="5" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="onSave">保存</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import crud from '@/mixins/crud';
import Farmland from '@/views/farmland-group/farmland';
import { getRegions, http_brand, http_farmland_group } from '@/api';

export default {
  name: 'FarmlandGroup',
  components: {
    Farmland
  },
  mixins: [crud],
  data() {
    return {
      componentName: 'FarmlandGroup',
      request: http_farmland_group,
      searchParams: {
        groupName: ''
      },
      regionList: [],
      tableData: [
      ],
      ruleForm: {
        id: undefined,
        groupName: '',
        imgUrl: '',
        regionCode: [],
        userId: '',
        directorUserName: '',
        phone: '',
        remarks: ''
      },
      rowItem: {},
      drawer: false,
      showFarmland: false
    }
  },
  created() {
    // 地区
    getRegions().then(res => {
      this.regionList = res.data.children
    })
  },
  methods: {
    add() {
      this.$router.push({
        path: '/farmland/add'
      })
    },
    onAddGroup() {
      this.ruleForm = {
        id: undefined,
        groupName: '',
        imgUrl: '',
        regionCode: [],
        userId: '',
        directorUserName: '',
        phone: '',
        remarks: ''
      }
      this.drawer = true
    },
    editGroup(item) {
      for (const key in this.ruleForm) {
        this.ruleForm[key] = item[key]
      }
      if(this.ruleForm.regionCode) {
        this.ruleForm.regionCode = this.ruleForm.regionCode.split(',')
      }
      this.drawer = true
    },
    onSave() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          http_farmland_group[this.ruleForm.id ? 'update' : 'create']({
            ...this.ruleForm,
            regionCode: this.ruleForm.regionCode.join(',')
          }).then(res => {
            this.$message.success(res.msg)
            this.drawer = false
            this.onSearch()
          })
        }
      })
    },
    editGroupDetail(item) {
      this.currentDetail = item
      http_farmland_group.totalArea(item.id).then(res => {
        this.currentDetail.areaSum = res.data
      })
      this.showFarmland = true
    },
    onBack() {
      this.showFarmland = false
    }
  }

}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style scoped lang="scss">
.farmland-page {
  position: relative;
  padding: 20px;
  background-color: #ffffff;
  height: 100%;
  .drawer-content {
    overflow: auto;
    height: 100%;
    background-color: #ffffff;
    width: 500px;
    padding: 20px;
    padding-top: 0;
    ::v-deep .el-form {
      .el-form-item {
        .el-input,
        .el-select {
          width: 100% !important;
        }
      }
    }
  }

  .el-form-item {
    margin-bottom: 22px;
  }
  .border {
    border-top: 1px solid #E8E8E8;
    margin-bottom: 27px;
  }
  .row-list {
    .row-box {
      width: 20%;
      padding: 12px 10px;
      .row-item {
        cursor: pointer;
        width: 100%;
        height: 200px;
        box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
        position: relative;
        border-radius: 14px;
        background-color: #58A768;
        overflow: hidden;
        .tag {
          position: absolute;
          left: 11px;
          top: -5px;
          width: 36px;
          height: 16px;
          background: #1AAD23;
          line-height: 16px;
          text-align: center;
          color: #ffffff;
          font-size: 12px;
        }
        .title-box {
          flex-grow: 1;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 48px;
          font-weight: 500;
          color: #FFFFFF;
          opacity: 1;
          background-repeat: no-repeat;
          background-size: 100% 100%;
          overflow: hidden;
        }
        .name-wrapper {
          padding: 2px 0 0 11px;
          height: 90px;
          width: 100%;
          background-color: #ffffff;
          border-radius:0 0 14px 14px;
          overflow: hidden;
          .avatar {
            position: absolute;
            width: 35px;
            height: 35px;
            box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
            opacity: 1;
            border: 1px solid #707070;
            border-radius: 50%;
            overflow: hidden;
            top: -12px;
            right: 14px;
            background-color: #ffffff;
            z-index: 999;
          }
          .name {
            font-size: 14px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
          .desc {
            margin-top: 5px;
            font-size: 12px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
        }
      }
    }
    .row-item1 {
      background-color: transparent !important;
      img {
        width: 78px;
        height: 62px;
      }
      .title {
        margin-top: 30px;
        font-size: 18px;

        font-weight: 400;
        line-height: 22px;
        color: #9A9A9A;
      }
    }
  }
  .header {
    .group-name {
      font-size: 14px;
      font-weight: 400;
      line-height: 24px;
      color: #9A9A9A;
      margin: 0 20px;
    }
    .top-back {
      cursor: pointer;
    }
    .title {
      font-size: 16px;

      font-weight: 400;
      color: #222222;
      line-height: 24px;
    }
    .value {
      font-size: 16px;
      font-family: Source Han Sans CN-Regular, Source Han Sans CN;
      font-weight: 400;
      color: #1AAD23;
      line-height: 14px;
      margin-right: 23px;
      font-weight: bold;
    }
  }
}
</style>
